package com.atguigu.srb.core.service.impl;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.enums.UserBindEnum;
import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.mapper.UserBindMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.UserBind;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.UserBindVO;
import com.atguigu.srb.core.service.UserBindService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author lupengning
 * @since 2021-05-27
 */
@Service
@Slf4j
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {

  @Resource
  private UserInfoMapper userInfoMapper;

  @Override
  public String commitBindUser(UserBindVO userBindVO, Long userId) {

    LambdaQueryWrapper<UserBind> queryWrapper = new LambdaQueryWrapper<>();
    // 不同的userId 相同的身份证，如果存在，不允许
    UserBind userBind1 = baseMapper.selectOne(
        queryWrapper.eq(UserBind::getIdCard, userBindVO.getIdCard())
            .ne(UserBind::getUserId, userId));
    Assert.isNull(userBind1, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);

    // 用户是否曾经写过绑定表单
    queryWrapper = new LambdaQueryWrapper<>();
    UserBind userBind = baseMapper.selectOne(queryWrapper.eq(UserBind::getUserId, userId));
    if (Objects.isNull(userBind)) {
      // 创建用户绑定记录
      userBind = new UserBind();
      BeanUtils.copyProperties(userBindVO,userBind);
      userBind.setUserId(userId);
      userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
      baseMapper.insert(userBind);
    } else{
      // 相同的user_id，取出并更新
      BeanUtils.copyProperties(userBindVO,userBind);
      baseMapper.updateById(userBind);
    }


    // 自动提交表单的参数
    Map<String, Object> paramMap = new HashMap<>();
    paramMap.put("agentId", HfbConst.AGENT_ID);
    paramMap.put("agentUserId", userId);
    paramMap.put("idCard",userBindVO.getIdCard());
    paramMap.put("personalName", userBindVO.getName());
    paramMap.put("bankType", userBindVO.getBankType());
    paramMap.put("bankNo", userBindVO.getBankNo());
    paramMap.put("mobile", userBindVO.getMobile());
    paramMap.put("returnUrl", HfbConst.USERBIND_RETURN_URL);
    paramMap.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);
    paramMap.put("timestamp", RequestHelper.getTimestamp());
    paramMap.put("sign", RequestHelper.getSign(paramMap));

    String formStr = FormHelper.buildForm(HfbConst.USERBIND_URL, paramMap);
    return formStr;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void notify(Map<String, Object> paramMap) {
    String bindCode = (String) paramMap.get("bindCode");
    // 会员ID
    String agentUserId = (String) paramMap.get("agentUserId");
    //根据user_id查询user_bind记录
    LambdaQueryWrapper<UserBind> queryWrapper = new LambdaQueryWrapper<>();
    UserBind userBind = baseMapper.selectOne(queryWrapper.eq(UserBind::getUserId, agentUserId));
    // 更新用户绑定表
    userBind.setBindCode(bindCode);
    userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
    baseMapper.updateById(userBind);
    //更新用户表
    UserInfo userInfo = userInfoMapper.selectById(agentUserId);
    userInfo.setBindCode(bindCode);
    userInfo.setName(userBind.getName());
    userInfo.setIdCard(userBind.getIdCard());
    userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
    userInfoMapper.updateById(userInfo);
  }
}
